os.loadAPI("/System/API/image")
local run = true
local xSize, ySize = term.getSize()
local xCenter = math.floor(xSize/2)
local yCenter = math.floor(ySize/2)
local obj = {}
local arg = {...}
local cursor = 3 
local check = -4
local imgPath = "/"
local progPath = "/"
local name = ""

local newObj = function(name,x1,y1,x2,y2)
 obj[name] = {}
 obj[name]["x1"] = x1
 obj[name]["y1"] = y1
 obj[name]["x2"] = x2
 obj[name]["y2"] = y2
end
 
local clear = function(color)
 term.setBackgroundColor(color)
 term.clear()
 term.setCursorPos(1,1)
end
 
local text = function(string,xPos,yPos)
 term.setCursorPos(xPos,yPos)
 term.write(string)
end
 
local function fadeOut(time)
        clear(colors.lightGray)
        sleep(time)
        clear(colors.gray)
        sleep(time)
        clear(colors.black)
        sleep(time)
        term.setCursorPos(1,1)
        term.setTextColor(colors.black)
end

local pastebin = function(paste,filename)
 local file = http.get("http://pastebin.com/raw.php?i="..paste)
 if file then
  file = file.readAll()
  h=fs.open(filename,"w")
  h.write(file)
  h.close()
 else
  clear(colors.black)
  term.setTextColor(colors.red)
  text("Connection error!",1,1)
  clear(colors.black)
  run=false
 end
end

local sysBar = function()
 sysbar.header("Package installer",colors.white,colors.gray)
 sysbar.draw(colors.black,colors.white,noty,true,false)
end

local redraw = function()
 clear(colors.black)
 term.setTextColor(colors.gray)
 text("Edit",xSize-4,4)
 text("Edit",xSize-4,7)
 text("Edit",xSize-4,10)
 if progPath ~= "/" and fs.exists(imgPath) and imgPath ~= "/" and name ~= "" then
  term.setTextColor(colors.lime)
  text("Install",xCenter-2,ySize-2)
 end
 term.setTextColor(colors.lightGray)
 text("File path:",1,4)
 text("Icon path:",1,7)
 text("App name:",1,10)
 term.setTextColor(colors.white)
 if progPath~="/" then
  if #progPath>xSize then
   text(progPath,xSize-#progPath+1,5)
   text("/..",1,5)
  else
   text(progPath,1,5)
  end
 end
 if name~="" then
  if #name>xSize then
   text(name,xSize-#name+1,11)
   text("/..",1,11)
  else
   text(name,1,11)
  end
 end
 if imgPath~="/" then
  if #imgPath>xSize then
   text(imgPath,xSize-#imgPath+1,8)
   text("/..",1,8)
  else
   text(imgPath,1,8)
  end
  if fs.exists(imgPath) then
   image.draw(xCenter-2,13,imgPath,6,4)
  elseif #imgPath==8 then
   pastebin(imgPath,"/pipastimg246103")
   image.draw(xCenter-2,13,"/pipastimg246103",6,4)
   shell.run("rm /pipastimg246103")
  end
 end
 sysBar()
end
 

local running = function()
 while run do
  event, side, x, y = os.pullEvent()
  if event == "mouse_click" then
   if sysbar.back(x,y) then
    term.clear()
    term.setCursorPos(1,1)
    run = false
   elseif sysbar.home(x,y) then
    term.clear()
    term.setCursorPos(1,1)
	home = false
    run = false
   elseif y>=4 and y<=5 then
    paintutils.drawLine(1,5,xSize,5,colors.black)
    term.setCursorPos(1,5)
    progPath=read()
    redraw()
   elseif y>=7 and y<=8 then
    paintutils.drawLine(1,8,xSize,8,colors.black)
    term.setCursorPos(1,8)
    imgPath=read()
    redraw()
   elseif y>=10 and y<=11 then
    paintutils.drawLine(1,11,xSize,11,colors.black)
    term.setCursorPos(1,11)
    name=read()
    redraw()
   elseif y==ySize-2 and name~="" and progPath ~= "/" and imgPath ~= "/" then
    if fs.exists(progPath) and fs.exists(imgPath) then
     shell.run("copy "..progPath.." /.ds/"..l_login.."/"..name..".apk/main")
     shell.run("copy "..imgPath.." /.ds/"..l_login.."/"..name..".apk/Resources/icon.png")
     term.setTextColor(colors.lime)
     text("Complete",xCenter-3,ySize-2)
     sleep(1)
     imgPath="/"
     progPath="/"
     name=""
     redraw()
    else
     if #progPath==8 and (fs.exists(imgPath) or #imgPath==8) then
      pastebin(progPath,"/.ds/"..l_login.."/"..name..".apk/main")
     elseif fs.exists(progPath) and (fs.exists(imgPath) or #imgPath==8) then
      shell.run("copy "..progPath.." /.ds/"..l_login.."/"..name..".apk/main")
     end
     if #imgPath==8 and (fs.exists(progPath) or #progPath==8) then
      pastebin(imgPath,"/.ds/"..l_login.."/"..name..".apk/Resources/icon.png")
     elseif fs.exists(imgPath) and (fs.exists(progPath) or #progPath==8) then
      shell.run("copy "..imgPath.." /.ds/"..l_login.."/"..name..".apk/Resources/icon.png")
     end
     term.setTextColor(colors.lime)
     text("Complete",xCenter-3,ySize-2)
     sleep(1)
     imgPath="/"
     progPath="/"
     name=""
     redraw()
    end
   end
  end
 end
end

redraw()
running()
clear(colors.black)